package org.ichow.scenegame.core
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Dictionary;

	/**
	 * ...
	 * @author M.ichow
	 */
	public class ToolTip extends Sprite
	{
		private static var instance:ToolTip;

		private var tooltips : Dictionary;
		
		private var _defSkin:Class;

		public function ToolTip()
		{
			tooltips = new Dictionary();
		}

		public static function getInstance() : ToolTip
		{
			if ( !instance )
				instance = new ToolTip();
			return instance;
		}

		public function createToolTip( type : String , skin : Class = null , style : Object = null ) : void
		{
			var tt : Sprite;
			if ( skin )
				tt = new skin() as Sprite;
			else
				tt = new defSkin() as Sprite;
			tooltips[ type ] = { style: style , tooltip: tt };
		}

		public function setText( type : String , txt : String = "" ) : void
		{
			var tt : Sprite = getToolTip( type );
			if ( !tt )
				return;
			var tf : TextField = tt.getChildByName( "txt" ) as TextField;
			tf.htmlText = "";
			tf.x = tf.y = 3;
			tf.multiline = false;
			tf.wordWrap = false;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.htmlText = txt;
			//check
			var style : Object = getStyle( type );
			if ( tf.width > style.maxWidth )
			{
				tf.width = style.maxWidth;
				tf.multiline = true;
				tf.wordWrap = true;
			}
			var bg : Sprite = tt.getChildByName( "bg_mc" ) as Sprite;
			bg.width = tf.width + 6;
			bg.height = tf.height + 6;
		}

		public function getToolTip( type : String ) : Sprite
		{
			return tooltips[ type ].tooltip ? tooltips[ type ].tooltip : null;
		}

		private function getStyle( type : String ) : Object
		{
			return tooltips[ type ].style ? tooltips[ type ].style : null;
		}
		
		public function get defSkin():Class 
		{
			return _defSkin;
		}
		
		public function set defSkin(value:Class):void 
		{
			_defSkin = value;
		}

	}

}